package me.zhengjie.search.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SearchMapper {
    /**
     * 从 title 中模糊查询联想（poem_poet、poem_user、recitation），最多 10 条，按 title 升序
     */
    @Select({
            "SELECT t.title",
            "FROM (",
            "  SELECT title FROM poem_poet    WHERE title LIKE CONCAT('%', #{keyword}, '%')",
            "  UNION",
            "  SELECT title FROM poem_user    WHERE title LIKE CONCAT('%', #{keyword}, '%')",
            "  UNION",
            "  SELECT title FROM recitation   WHERE title LIKE CONCAT('%', #{keyword}, '%')",
            ") AS t",
            "ORDER BY t.title ASC",
            "LIMIT 10"
    })
    List<String> findTitles(@Param("keyword") String keyword);
}
